package com.whmnx.my.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.whmnx.base.bean.LocationBean;
import com.whmnx.my.BR;
import com.whmnx.my.R;
import com.whmnx.my.databinding.ActivityLocSearchBinding;
import com.whmnx.my.ui.adapter.PoiAdapter;
import com.whmnx.my.ui.adapter.SearchResultAdapter;
import com.whmnx.my.ui.base.PoiSearchTask;
import com.whmnx.my.ui.viewmodel.SearchModel;

import me.goldze.mvvmhabit.base.BaseActivity;

/**
 * author : huang
 * date   : 2020/11/17 0017 11:03
 * version: 1.0
 */
public class LocationSearchActivity extends BaseActivity<ActivityLocSearchBinding, SearchModel> implements View.OnClickListener, AdapterView.OnItemClickListener {
    private TextView tvSearch,tvCancel;
    private EditText etSearch;
    private ListView listView;
    private ImageView ivX;
    private String data;
    private String city;
    private SearchResultAdapter searchResultAdapter;
    private PoiAdapter adapter;
    private double latitude,longitude;
    private Object item;


    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_loc_search;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        initView();
    }

    private void initView() {
      //  tvSearch = (TextView)findViewById(R.id.tv_cancel);
        etSearch = (EditText) findViewById(R.id.et_input);
        listView = (ListView)findViewById(R.id.listView);
        ivX = (ImageView) findViewById(R.id.iv_x);
        tvCancel = findViewById(R.id.tv_cancel);
        city = getIntent().getStringExtra("city");
        latitude = getIntent().getDoubleExtra("latitude",0);
        longitude =  getIntent().getDoubleExtra("longitude",0);
        ivX.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
        tvCancel.setOnClickListener(v -> finish());

        adapter = new PoiAdapter(this);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(editable.toString())) {
                    ivX.setVisibility(View.GONE);
                } else {
                    ivX.setVisibility(View.VISIBLE);
                    //第二个参数传入null或者“”代表在全国进行检索，否则按照传入的city进行检索
                    String eInput =  etSearch.getText().toString();
                    PoiSearchTask.getInstance(LocationSearchActivity.this).setAdapter(adapter).onSearchText(eInput, city);

                  /*  InputtipsQuery inputquery = new InputtipsQuery(eInput, city);
                    inputquery.setCityLimit(false);//限制在当前城市
                    Inputtips inputTips = new Inputtips(LocationSearchActivity.this, inputquery);*/

                   /* inputTips.setInputtipsListener(new Inputtips.InputtipsListener() {
                        @Override
                        public void onGetInputtips(List<Tip> list, int i) {
                            searchResultAdapter = new SearchResultAdapter((ArrayList<Tip>) list, LocationSearchActivity.this);
                            listView.setAdapter(searchResultAdapter);
                            searchResultAdapter.notifyDataSetChanged();
                        }
                    });
                    inputTips.requestInputtipsAsyn();*/
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        etSearch.setText("");
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        LocationBean item = (LocationBean) adapter.getItem(position);
        Intent intent = new Intent();
        intent.putExtra("item", item);
        setResult(1, intent);
        finish();
    }
}
